<?php

namespace app\bews\controller;

use app\bews\model\Base;
use think\app;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Bewadmin extends Base
{
    public function adminInfo(){
        $lists = Db::table("bew_admin_user")->select();
        $group = Db::table("bew_admin_user_group")->select()->toArray();
        $tmp_group = [];
        foreach ($group as $group_v){
            $tmp_group[$group_v['group_id']] = $group_v;
        }
        View::assign([
            'lists'=>$lists,
            'group'=>$tmp_group
        ]);
        return View::fetch();
    }

    public function adminAdd(){
        if(Request::isPost()){
//            print_r(input('post.'));
            $name = input('post.name');
            $account = input('post.account');
            $password = input('post.password');
            $mobile = input('post.mobile');
            $status = input('post.status');
            $group_id = input('post.group_id');
            if($group_id == null){
                echo json_encode(['code'=>1,'msg'=>'请选择部门']);
                exit();
            }
            $insert = Db::table('bew_admin_user')->insert([
                'account'=>$account,
                'password'=>$password,
                'name'=>$name,
                'mobile'=>$mobile,
                'status'=>$status,
                'group_id'=>$group_id
            ]);
            if(empty($insert)){
                echo json_encode(['code'=>1,'msg'=>'添加失败,请重试']);
                exit();
            }else{
                echo json_encode(['code'=>0,'msg'=>'添加成功']);
            }
        }else{
            $groups = Db::table("bew_admin_user_group")->where('status',1)->select();
            View::assign([
                'groups'=>$groups
            ]);
            return View::fetch();
        }
    }

    public function adminModify(){
        if(Request::isPost()){
            $uid = input('post.uid');
            $group_id = input('post.group_id');
            $account = input('post.account');
            $password = input('post.password');
            $mobile = input('post.mobile');
            $status = input('post.status',1);
            if($group_id == null){
                echo json_encode(['code'=>1,'msg'=>'请选择部门']);
                exit();
            }
            $update = Db::table("bew_admin_user")->where('uid',$uid)->update([
                'group_id'=>$group_id,
                'account'=>$account,
                'password'=>$password,
                'mobile'=>$mobile,
                'status'=>$status,
            ]);
            if($update == null){
                echo json_encode(['code'=>1,'msg'=>'修改失败,请重试']);
                exit();
            }
            echo json_encode(['code'=>0,'msg'=>'修改成功']);
        }else{
            $uid = input('get.uid');
            if(empty($uid)){
                $this->error("请传入uid");
                exit();
            }
            $find = Db::table("bew_admin_user")->where('uid',$uid)->find();
            View::assign([
                'find'=>$find
            ]);
            $groups = Db::table("bew_admin_user_group")->where('status',1)->select();
            View::assign([
                'group'=>$groups
            ]);
            return View::fetch();
        }
    }

    public function adminDel(){
        $uid = input('post.uid');
        if(empty($uid)){
            $this->error("请传入uid");
            exit();
        }
        $del = Db::table("bew_admin_user")->where('uid',$uid)->delete();
        if($del == null){
            echo json_encode(['code'=>1,'msg'=>'删除失败,请重试']);
            exit();
        }
        echo json_encode(['code'=>0,'msg'=>'删除成功']);
        return View::fetch();
    }

    public function groupInfo(){
        $lists = Db::table("bew_admin_user_group")->select();
        View::assign([
            'lists'=>$lists
        ]);
        return View::fetch();
    }

    public function groupAdd(){
        if(Request::isPost()){
//            print_r(input('post.'));
            $group_name = input('post.group_name');
            $status = input('post.status');
            $menus = input('post.menu/a');  //强制转换为数组
            if($menus != null){
                $menus_json = json_encode(array_keys($menus));
            }else{
                echo json_encode(['code'=>1,'msg'=>'请选择权限']);
            }
            $insert = Db::table('bew_admin_user_group')->insert([
                'group_name'=>$group_name,
                'status'=>$status,
                'rights'=>$menus_json
            ]);
            if(empty($insert)){
                echo json_encode(['code'=>1,'msg'=>'添加失败,请重试']);
                exit();
            }else{
                echo json_encode(['code'=>0,'msg'=>'添加成功']);
            }
        }else{
            $menus = Db::table("bew_admin_sys_menu")->where('status',1)->order('type,sort DESC')->select()->toArray();
            $tree = [];
//        print_r($menus);
            foreach ($menus as $menus_v){
                if($menus_v['parent_id'] == 0){
                    $tree[$menus_v['smid']] = $menus_v;
                }else{
                    $tree[$menus_v['parent_id']]['children'][] = $menus_v;
                }
            }
            View::assign([
                'menus'=>$tree
            ]);
            return View::fetch();
        }
    }

    public function groupModify(){
        if(Request::isPost()){
            $menus = input('post.menu/a');  //强制转换为数组
            if($menus != null){
                $menus_json = json_encode(array_keys($menus));
            }else{
                echo json_encode(['code'=>1,'msg'=>'请选择权限']);
            }
            $group_id = input('post.group_id');
            $group_name = input('post.group_name');
            $status = input('post.status',1);
            $update = Db::table("bew_admin_user_group")->where('group_id',$group_id)->update([
                'group_name'=>$group_name,
                'status'=>$status,
                'rights'=>$menus_json
            ]);
            if($update == null){
                echo json_encode(['code'=>1,'msg'=>'修改失败,请重试']);
                exit();
            }
            echo json_encode(['code'=>0,'msg'=>'修改成功']);
        }else{
            /*$group_id = input('get.group_id');
            if(empty($group_id)){
                $this->error("请传入uid");
                exit();
            }
            $find = Db::table("bew_admin_user_group")->where('group_id',$group_id)->find();
            if($find != null && $find['rights']){
                $find['rights'] = json_decode($find['rights']);
            }
//            echo json_decode(implode($find['rights']));
            View::assign([
                'find'=>$find
            ]);
            $menus = Db::table("bew_admin_sys_menu")->where('status',1)->order('type,sort DESC')->select()->toArray();
            $tree = [];
//        print_r($menus);
            foreach ($menus as $menus_v){
                if($menus_v['parent_id'] == 0){
                    $tree[$menus_v['smid']] = $menus_v;
                }else{
                    $tree[$menus_v['parent_id']]['children'][] = $menus_v;
                }
            }
            View::assign([
                'menus'=>$tree
            ]);
            return View::fetch();*/
            $group_id = (int)input('get.group_id');
            $group = Db::table('bew_admin_user_group')->where('group_id',$group_id)->find();
            if($group && $group['rights']){
                $group['rights'] = json_decode($group['rights']);
            }

            $menus = Db::table('bew_admin_sys_menu')->order('type,sort desc')->where('status','=',1)->select();
            foreach($menus as $menus_v){
                if($menus_v['parent_id'] == 0){
                    $menu[$menus_v['smid']] = $menus_v;
                }else{
                    $menu[$menus_v['parent_id']]['children'][] = $menus_v;
                }
            }
            View::assign([
                'group' => $group,
                'menus' => $menu
            ]);
            $find = Db::table("bew_admin_user_group")->where('group_id',$group_id)->find();
            if($find != null && $find['rights']){
                $find['rights'] = json_decode($find['rights']);
            }
//            echo json_decode(implode($find['rights']));
            View::assign([
                'find'=>$find
            ]);
            return View::fetch();
        }
    }

    public function groupDel(){
        $group_id = input('post.group_id');
        if(empty($group_id)){
            $this->error("请传入部门ID");
            exit();
        }
        $del = Db::table("bew_admin_user_group")->where('group_id',$group_id)->delete();
        if($del == null){
            echo json_encode(['code'=>1,'msg'=>'删除失败,请重试']);
            exit();
        }
        echo json_encode(['code'=>0,'msg'=>'删除成功']);
        return View::fetch();
    }
}